﻿package com.gengine.media.core.play
{
    import com.gengine.debug.*;
    import com.gengine.media.events.*;
    import flash.errors.*;
    import flash.events.*;
    import flash.media.*;
    import flash.net.*;
    import flash.utils.*;

    public class MP3PlayCore extends MediaPlayCore implements IMediaPlayCore
    {
        private var _sound:Sound;
        private var _context:SoundLoaderContext;
        private var _soundChanel:SoundChannel;
        private var _timer:Timer;
        private var _pauseTime:Number = 0;
        private var _isPlayStart:Boolean = false;

        public function MP3PlayCore(param1:String = null, param2:Number = 1000, param3:Boolean = false, param4:Number = 250, param5:Object = null)
        {
            var url:* = param1;
            var bufferTime:* = param2;
            var checkPolicyFile:* = param3;
            var updateDelay:* = param4;
            var callback:* = param5;
            super(url, bufferTime, callback);
            this._timer = new Timer(updateDelay);
            this._timer.addEventListener(TimerEvent.TIMER, this.playingStateTimerEventHandler);
            this._context = new SoundLoaderContext(bufferTime, checkPolicyFile);
            this._sound = new Sound();
            this._sound.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorEventHandler);
            this._sound.addEventListener(Event.OPEN, this.openEventHandler);
            this._sound.addEventListener(ProgressEvent.PROGRESS, this.progressEventHandler);
            this._sound.addEventListener(Event.COMPLETE, this.completeEventHandler);
            this._sound.addEventListener(Event.ID3, this.id3DataEventHandler);
            try
            {
                this._sound.load(new URLRequest(url), this._context);
            }
            catch (err:IOError)
            {
                Log.system("流加载IO错误：" + err.message);
                ;
            }
            catch (err:SecurityError)
            {
                Log.system("流加载安全性错误：" + err.message);
            }
            return;
        }// end function

        override public function set volume(param1:Number) : void
        {
            var _loc_2:SoundTransform = null;
            super.volume = param1;
            if (this._soundChanel)
            {
                _loc_2 = this._soundChanel.soundTransform;
                _loc_2.volume = _mute ? (0) : (param1);
                this._soundChanel.soundTransform = _loc_2;
            }
            return;
        }// end function

        override public function play() : void
        {
            if (!playing)
            {
                super.play();
                this.soundPlayAction();
            }
            else
            {
                Log.system("声音正处于播放状态，无法重复进行播放动作！");
            }
            return;
        }// end function

        override public function pause() : void
        {
            if (playing)
            {
                super.pause();
                this.soundStopAction();
                callbackFunction(MediaPlayEvent.PAUSE);
            }
            else
            {
                Log.system("声音正处于非播放状态，无法进行暂停播放动作！");
            }
            return;
        }// end function

        override public function resume() : void
        {
            if (paused)
            {
                super.play();
                this.soundPlayAction(this._pauseTime);
                callbackFunction(MediaPlayEvent.RESUME);
            }
            else
            {
                Log.system("声音正处于非暂停状态，无法进行暂停播放动作！");
            }
            return;
        }// end function

        override public function stop() : void
        {
            if (!stopped)
            {
                super.stop();
                this.soundStopAction(true);
                callbackFunction(MediaPlayEvent.STOP);
            }
            else
            {
                Log.system("声音正处于停止状态，无法重复进行停止播放动作！");
            }
            return;
        }// end function

        override public function seek(param1:Number = 0) : void
        {
            var _loc_2:Boolean = false;
            if (!stopped)
            {
                if (param1 < 0)
                {
                    param1 = 0;
                    ;
                }
                if (param1 >= _totalTime)
                {
                    param1 = _totalTime - 0.001;
                    ;
                }
                _loc_2 = playing;
                if (_loc_2)
                {
                    this.pause();
                    ;
                }
                this._pauseTime = param1 * 1000;
                callbackFunction(MediaPlayEvent.SEEK);
                if (_loc_2)
                {
                    this.resume();
                    ;
                }
            }
            else
            {
                Log.system("声音正处于停止状态，无法进行播放进度搜索动作！");
            }
            return;
        }// end function

        override public function seekPercent(param1:Number = 0) : void
        {
            var _loc_2:* = param1 * _totalTime;
            this.seek(_loc_2);
            return;
        }// end function

        override public function dispose() : void
        {
            if (this._sound != null)
            {
                this._sound.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorEventHandler);
                this._sound.removeEventListener(Event.OPEN, this.openEventHandler);
                this._sound.removeEventListener(ProgressEvent.PROGRESS, this.progressEventHandler);
                this._sound.removeEventListener(Event.COMPLETE, this.completeEventHandler);
                this._sound.removeEventListener(Event.ID3, this.id3DataEventHandler);
                this._sound = null;
            }
            if (this._timer != null)
            {
                this._timer.removeEventListener(TimerEvent.TIMER, this.playingStateTimerEventHandler);
                this._timer.stop();
                this._timer = null;
            }
            if (this._soundChanel != null)
            {
                this._soundChanel.removeEventListener(Event.SOUND_COMPLETE, this.soundCompleteEventHandler);
                this._soundChanel = null;
            }
            super.dispose();
            return;
        }// end function

        private function soundPlayAction(param1:Number = 0) : void
        {
            if (!this._sound || !this._timer)
            {
                return;
            }
            this._timer.start();
            this._soundChanel = this._sound.play(param1);
            this._soundChanel.addEventListener(Event.SOUND_COMPLETE, this.soundCompleteEventHandler);
            this.volume = _volume;
            return;
        }// end function

        private function soundStopAction(param1:Boolean = false) : void
        {
            if (!this._timer || !this._soundChanel || !this._sound)
            {
                return;
            }
            this._timer.stop();
            this._soundChanel.stop();
            this._soundChanel.removeEventListener(Event.SOUND_COMPLETE, this.soundCompleteEventHandler);
            if (param1)
            {
                this._pauseTime = 0;
                try
                {
                    this._sound.close();
                }
                catch (err:IOError)
                {
                }
            }
            else
            {
                this._pauseTime = this._soundChanel.position;
            }
            this._soundChanel = null;
            return;
        }// end function

        private function ioErrorEventHandler(event:IOErrorEvent) : void
        {
            callbackFunction(MediaPlayEvent.IO_ERROR, event.text);
            return;
        }// end function

        private function openEventHandler(event:Event) : void
        {
            if (!this._context)
            {
                return;
            }
            var _loc_2:* = _totalTime * 1000;
            if (this._context.bufferTime > _loc_2)
            {
                this._context.bufferTime = _loc_2;
                ;
            }
            callbackFunction(MediaPlayEvent.OPEN);
            return;
        }// end function

        private function progressEventHandler(event:ProgressEvent) : void
        {
            _loadPercentage = event.bytesLoaded / event.bytesTotal;
            callbackFunction(MediaPlayEvent.PROGRESS);
            return;
        }// end function

        private function completeEventHandler(event:Event) : void
        {
            callbackFunction(MediaPlayEvent.COMPLETE);
            return;
        }// end function

        private function id3DataEventHandler(event:Event) : void
        {
            if (!this._sound || !this._sound.id3)
            {
                return;
            }
            _mediaData = this._sound.id3;
            _mediaData.type = "mp3";
            callbackFunction(MediaPlayEvent.INIT);
            return;
        }// end function

        private function playingStateTimerEventHandler(event:TimerEvent) : void
        {
            if (!this._sound || !this._soundChanel)
            {
                return;
            }
            _buffering = this._sound.isBuffering;
            _playheadTime = this._soundChanel.position / 1000;
            _totalTime = this._sound.bytesTotal / (this._sound.bytesLoaded / this._sound.length) / 1000;
            if (_buffering)
            {
                _bufferPercentage = this._sound.length / (this._soundChanel.position + _bufferTime);
                if (_bufferPercentage < 0)
                {
                    _bufferPercentage = 0;
                    ;
                }
                callbackFunction(MediaPlayEvent.BUFFERING);
            }
            else
            {
                if (!this._isPlayStart && _playheadTime > 0)
                {
                    this._isPlayStart = true;
                    callbackFunction(MediaPlayEvent.PLAY);
                }
                callbackFunction(MediaPlayEvent.PLAYING);
            }
            return;
        }// end function

        private function soundCompleteEventHandler(event:Event) : void
        {
            this.stop();
            callbackFunction(MediaPlayEvent.PLAY_COMPLETE);
            return;
        }// end function

    }
}
